package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/OperrateLogsController", produces = MediaType.APPLICATION_JSON)
public class OperrateLogsController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(OperrateLogsController.class);

    @Autowired
    private IOperrateLogsService iOperrateLogsService;
    @Autowired
    private ICommonService iCommonService;

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryOperatelogsPage")
    public ApiResultDTO queryOperatelogsPage(@RequestHeader("X-Token") String tokenid,
                                             @RequestParam(value = "page") String page,
                                             @RequestParam(value = "limit") String limit,
                                             @RequestParam(value = "agencyId") String agencyId,
                                             @RequestParam(value = "endDate",required = false) String endDate,
                                             @RequestParam(value = "startDate",required = false) String startDate,
                                             @RequestParam(value = "ueserName",required = false) String ueserName,
                                             @RequestParam(value = "operateName",required = false) String operateName){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<String> childrenId = iCommonService.getAgencyChildrenId(userDTO, agencyId);
            Map<String, Object> queryParam = new HashMap<>(3);
            String province = getUser(tokenid).getProvince();
            queryParam.put("mofDivCode",province);
            queryParam.put("endDate",endDate);
            queryParam.put("startDate",startDate);
            queryParam.put("ueserName",ueserName);
            queryParam.put("operateName",operateName);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page,limit);
            PageDTO<OperrateLogs> pageDTO = iOperrateLogsService.queryOperatelogsPage(paginationDTO, queryParam, childrenId);
            return ApiResultDTO.success(pageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "获取数据异常");
        }

    }
}
